#include "sLibrary.h"
#include "sNamedObjectContainer.h"

#include "sLocalizationResource.h"
#include "sTextureResource.h"
#include "sComponentResource.h"
#include "sLevelResource.h"

#include "sTexturePreset.h"
#include "sFontPreset.h"


namespace sgui
{

	CONTAINER_WRAPPER(res::sResource)& sLibrary::getResources()
	{
		return m_resources;
	}

	CONTAINER_WRAPPER(preset::sPreset)& sLibrary::getPresets()
    {
        return m_presets;
    }

	DEFINE_FABRIC(fbLocalizationResource, 
		new res::sLocalizationResource(gui, *file.getName())
	);
	DEFINE_FABRIC(fbTextureResource, 
		new res::sTextureResource(gui, *file.getName())
	);
	DEFINE_FABRIC(fbFontResource, 
		new res::sFontResource(gui, *file.getName())
	);
	DEFINE_FABRIC(fbComponentResource, 
		new res::sComponentResource(gui, *file.getName())
	);
	DEFINE_FABRIC(fbLevelResource, 
		new res::sLevelResource(gui, *file.getName())
	);

	// ----------------------------------- fbResource: fabric -------------------------------------
	DEFINE_FABRIC_BEGIN(fbResource)

		const std::wstring &name = file.GetAsString();

		std::map<std::wstring, const sNamedObjectFabric*>	m_fabrics;

		m_fabrics.insert(std::pair<std::wstring, const sNamedObjectFabric*>(
			res::sLocalizationResource::RESOURCE_NAME, &fbLocalizationResource
		));
		m_fabrics.insert(std::pair<std::wstring, const sNamedObjectFabric*>(
			res::sTextureResource::RESOURCE_NAME, &fbTextureResource
		));
		m_fabrics.insert(std::pair<std::wstring, const sNamedObjectFabric*>(
			res::sFontResource::RESOURCE_NAME, &fbFontResource
		));
		m_fabrics.insert(std::pair<std::wstring, const sNamedObjectFabric*>(
			res::sComponentResource::RESOURCE_NAME, &fbComponentResource
		));
		m_fabrics.insert(std::pair<std::wstring, const sNamedObjectFabric*>(
			res::sLevelResource::RESOURCE_NAME, &fbLevelResource
		));

		std::map<std::wstring, const sNamedObjectFabric*>::const_iterator ps = m_fabrics.find(name);

		if (ps != m_fabrics.end())
		{
			return ps->second->CreateObject(gui, file);
		}

        return NULL;

	DEFINE_FABRIC_END(fbResource);
	// -----------------------------------------------------------------------------------------

	DEFINE_FABRIC(fbFontPreset, 
		new preset::sFontPreset(gui, *file.getName(), 
			*(res::sFontResource*)gui.getGuiObject(file[L"resource"].GetAsInt()))
	);
	DEFINE_FABRIC(fbTexturePreset, 
		new preset::sTexturePreset(gui, *file.getName(), 
			*(res::sTextureResource*)gui.getGuiObject(file[L"resource"].GetAsInt()))
	);

	// ----------------------------------- fbPreset: fabric -------------------------------------
	DEFINE_FABRIC_BEGIN(fbPreset)

		const std::wstring &name = file.GetAsString();

		std::map<std::wstring, const sNamedObjectFabric*>	m_fabrics;

		m_fabrics.insert(std::pair<std::wstring, const sNamedObjectFabric*>(
			preset::PRESET_FONT, &fbFontPreset
		));
		m_fabrics.insert(std::pair<std::wstring, const sNamedObjectFabric*>(
			preset::PRESET_TEXTURE, &fbTexturePreset
		));

		std::map<std::wstring, const sNamedObjectFabric*>::const_iterator ps = m_fabrics.find(name);

		if (ps != m_fabrics.end())
		{
			return ps->second->CreateObject(gui, file);
		}

        return NULL;

	DEFINE_FABRIC_END(fbPreset);
	// --------------------------------------------------------------------------------------------

    void sLibrary::load(const db::Record &file)
    {
		sNamedObject::load(file);

        m_resources.load(file[L"resources"], fabric::fbResource);
		m_presets.load(file[L"presets"], fabric::fbPreset);
    }

    void sLibrary::save(db::Record &file)
    {
		sNamedObject::save(file);

        m_resources.save(file[L"resources"]);
        m_presets.save(file[L"presets"]);
    }

    void sLibrary::clear()
    {
        m_resources.clear();
    }

#ifdef _EDITOR
	void sLibrary::exportRTTI(rtti::sInfoList& list)
	{
		sNamedObject::exportRTTI(list);
	}
#endif

    sLibrary::sLibrary(sGUI& gui, const std::wstring& name) :
		sNamedObject(gui, name), 
		m_resources(gui), 
		m_presets(gui)
    {
        //
    }
};